# Set console encoding to UTF-8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

Write-Host "========================================" -ForegroundColor Green
Write-Host "Downloading Git from Chinese Mirror" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green

# Create temp directory
$tempDir = "$env:TEMP\git_install"
if (!(Test-Path $tempDir)) {
    New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
}

# Try different Chinese mirrors for Git
$mirrors = @(
    "https://npm.taobao.org/mirrors/git-for-windows/2.43.0.windows.1/Git-2.43.0-64-bit.exe",
    "https://mirrors.huaweicloud.com/git-for-windows/2.43.0.windows.1/Git-2.43.0-64-bit.exe",
    "https://mirrors.aliyun.com/git-for-windows/2.43.0.windows.1/Git-2.43.0-64-bit.exe"
)

$installerPath = "$tempDir\GitInstaller.exe"
$downloadSuccess = $false

foreach ($mirror in $mirrors) {
    try {
        Write-Host "Trying mirror: $mirror" -ForegroundColor Cyan
        Write-Host "Downloading Git installer..." -ForegroundColor Yellow
        
        # Download with progress
        $webClient = New-Object System.Net.WebClient
        $webClient.DownloadFile($mirror, $installerPath)
        
        if (Test-Path $installerPath) {
            $fileSize = (Get-Item $installerPath).Length
            if ($fileSize -gt 1000000) { # Check if file is larger than 1MB
                Write-Host "Download completed successfully!" -ForegroundColor Green
                Write-Host "File size: $([math]::Round($fileSize/1MB, 2)) MB" -ForegroundColor Green
                $downloadSuccess = $true
                break
            } else {
                Write-Host "Downloaded file seems too small, trying next mirror..." -ForegroundColor Yellow
                Remove-Item $installerPath -Force -ErrorAction SilentlyContinue
            }
        }
    } catch {
        Write-Host "Failed to download from this mirror, trying next..." -ForegroundColor Red
        Remove-Item $installerPath -Force -ErrorAction SilentlyContinue
    }
}

if ($downloadSuccess) {
    Write-Host "========================================" -ForegroundColor Green
    Write-Host "Git installer downloaded successfully!" -ForegroundColor Green
    Write-Host "========================================" -ForegroundColor Green
    Write-Host ""
    Write-Host "Installing Git..." -ForegroundColor Cyan
    Write-Host "Please follow the default settings in the installation window" -ForegroundColor Yellow
    Write-Host "After installation, please run the upload script again" -ForegroundColor Yellow
    Write-Host ""
    
    # Start installer
    Start-Process -FilePath $installerPath -Wait
    
    Write-Host "Git installation completed!" -ForegroundColor Green
    Write-Host "Please restart your PowerShell and run the upload script again" -ForegroundColor Yellow
} else {
    Write-Host "========================================" -ForegroundColor Red
    Write-Host "Failed to download Git from all mirrors!" -ForegroundColor Red
    Write-Host "========================================" -ForegroundColor Red
    Write-Host ""
    Write-Host "Please download Git manually:" -ForegroundColor Yellow
    Write-Host "1. Visit: https://git-scm.com/download/win" -ForegroundColor White
    Write-Host "2. Download the Windows version" -ForegroundColor White
    Write-Host "3. Install with default settings" -ForegroundColor White
    Write-Host "4. Restart PowerShell and run the upload script" -ForegroundColor White
    Write-Host ""
    Write-Host "Alternative download links:" -ForegroundColor Cyan
    Write-Host "- https://github.com/git-for-windows/git/releases" -ForegroundColor Blue
    Write-Host "- https://gitee.com/mirrors/git-for-windows" -ForegroundColor Blue
}

Read-Host "Press any key to exit" 